"""
Case Type   : 资源池化
Case Name   : 扩容工具含有XML时gs_expansion -U {user} -G {user} -X {xml} -h {ip}指定参数无问题
Create At   : 2024.10
Owner       : chen
Description :
    1.指定参数无问题
    gs_expansion -U {user} -G {user} -X {xml} -h {ip}
    2.检查扩容情况
    cm_ctl -query -Cv
    3.恢复环境
    3.1执行缩容
    gs_dropnode -U {user} -G {user} -h {ip}  
    3.2恢复集群到初始状态
    cm_ctl switchover -a
Expect      :
    1.执行扩容成功
    2.集群状态正常，新增一个节点
    3.恢复成功
    3.1缩容成功
    3.2恢复成功
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.pri_root_node = Node('PrimaryRoot')
        self.sta2_node = Node('Standby2DbUser')
        self.commsh = CommonSH('PrimaryDbUser')
        self.db_host = self.sta2_node.db_host
        self.db_name = self.pri_node.ssh_user
        text = "卸载备二"
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 600
            spawn gs_dropnode -U {self.db_name} -G {self.db_name} \
            -h {self.db_host}
            expect "*drop the target node (yes/no)?*"
            send "yes\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Success to drop the target nodes', res)
        res = self.sta2_node.sh(f'source {macro.DB_ENV_PATH}; ' \
            'gs_uninstall --delete-data').result()
        self.log.info(res)

    def test_shared_storage(self):
        text = "-----step1: 指定参数无问题 gs_expansion -U {user} -G {user} -X {xml} -h {ip}"  \
            ' expect: 执行成功-----'
        self.log.info(text)
        xml_path = macro.DB_XML_PATH
        cmd = f''' gs_expansion \
            -U {self.db_name} -G {self.db_name} -X {xml_path} -h {self.db_host}'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 300
            spawn {cmd}
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*lease repeat for database*:"
            send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd)
        res = self.pri_root_node.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn(f'{self.db_host}:\tSuccess', res)

        text = '-----step2: 检查扩容情况 expect: 集群状态正常，新增一个节点-----'
        self.log.info(text)
        res = self.commsh.exec_cm_ctl('query', '-Cv')
        self.log.info(res)
        node_arr = res.splitlines()[-1].split('|')
        for node_mes in node_arr:
            tmp = node_mes.strip()
            self.assertIn('Normal', node_mes)

    def tearDown(self):
        text = '-----step3: 恢复环境-----'
        self.log.info(text)
        text = '-----step3.1: 执行缩容 expect: 缩容成功-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 600
            spawn gs_dropnode -U {self.db_name} -G {self.db_name} \
            -h {self.db_host}
            expect "*drop the target node (yes/no)?*"
            send "yes\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Success to drop the target nodes', res)
        res = self.sta2_node.sh(f'source {macro.DB_ENV_PATH}; ' \
            'gs_uninstall --delete-data').result()
        self.log.info(res)
        text = '-----step3.2: 恢复集群到初始状态 expect: 恢复成功-----'
        self.log.info(text)
        res = self.commsh.exec_cm_ctl('switchover', '-a')
        self.log.info(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')